using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using System.Configuration;


namespace eBirdAttend
{
    public partial class Designation : UserControl
    {

        public string strCon = Properties.Settings.Default["ConString"].ToString();
        SqlConnection sqlCon = new SqlConnection();
        SqlCommand sqlCmd = new SqlCommand();
        String strBtnCaption = "";

        public Designation()
        {
            InitializeComponent();
            sqlCon.ConnectionString = strCon;
        }


        // Function name   : btnAdd_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnAdd_Click(object sender, EventArgs e)
        { 
            ClearControls();
            groupBox1.Visible = true;
            strBtnCaption = "Add";
            groupBox1.Text = "ADD A DESIGNATION";
          

            sqlCmd.CommandText = "select max(desid) from designation";
            sqlCmd.Connection = sqlCon;
            sqlCon.Open();
            SqlDataReader sqlReader;
            sqlReader = sqlCmd.ExecuteReader();
            if (sqlReader.Read())
            {
                if (sqlReader[0].ToString() == "")
                {

                }
                else
                {
                    txtDesigId.Text = Convert.ToString(Convert.ToInt32(sqlReader[0].ToString()) + 1);
                }
            }
            else
            {
                txtDesigId.Text = "1";
            }

            if (txtDesigId.Text == "")
            {
                txtDesigId.Text = "1";
            }
            txtDesigName.Focus();
            sqlCon.Close(); 
        }

        private void Designation_Load(object sender, EventArgs e)
        {
            designationTableAdapter1.Fill(designationDataSet.Designation);
            designationview.Columns[0].Visible = false;
            designationview.Columns[1].Width = 150;
            designationview.Columns[2].Width = 250;
        }


        // Function name   : btnEdit_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnEdit_Click(object sender, EventArgs e)
        {  
            ClearControls();
            if (designationview.SelectedRows.Count == 0)
            { MessageBox.Show("Please select a row for edit"); return; }

            if ((designationview.RowCount - 1) == designationview.SelectedRows[0].Index)
            {
                MessageBox.Show("No content to edit");
                groupBox1.Visible = false;
                ClearControls();
                return;
            }

            groupBox1.Visible = true;
            strBtnCaption = "Edit";
            groupBox1.Text = "EDIT A DESIGNATION";
       
          
            //MessageBox.Show(dataGridView1.SelectedRows[0].Cells[1].Value.ToString());
            txtDesigId.Text = designationview.SelectedRows[0].Cells[0].Value.ToString();
            txtDesigName.Text = designationview.SelectedRows[0].Cells[1].Value.ToString();
            txtDesigDescr.Text = designationview.SelectedRows[0].Cells[2].Value.ToString();

            txtDesigId.Enabled = false;
            txtDesigName.Focus();
        }


        // Function name   : btnDelete_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (designationview.SelectedRows.Count == 0)
                { MessageBox.Show("Please select a row for delete"); return; }

                DialogResult _dlg = MessageBox.Show("Are you sure you want to delete?", "Warning", MessageBoxButtons.YesNo);
                if (_dlg == DialogResult.No)
                {
                    return;
                }
                else
                {
                    string strDesId = designationview.SelectedRows[0].Cells[0].Value.ToString();
                    while (designationview.Rows.Count > 1)
                        designationview.Rows.RemoveAt(0);
                    sqlCmd.CommandText = "delete from  Designation where DesId ='" + strDesId + "'";
                    sqlCmd.Connection = sqlCon;
                    sqlCon.Open();
                    sqlCmd.ExecuteNonQuery();
                    sqlCon.Close();
                    designationTableAdapter1.Fill(designationDataSet.Designation);

                    MessageBox.Show("Successfuly Deleted");
                }
            }
            catch (Exception ex)
            {
            }
        }


        // Function name   : btnSave_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtDesigName.Text == "")
            {
                MessageBox.Show("Enter the designation name");
                txtDesigName.Focus();
            }
            else
            {
                try
                {
                    //save the new designation details
                    if (strBtnCaption == "Add")
                    {
                        while (designationview.Rows.Count > 1)
                            designationview.Rows.RemoveAt(0);
                        sqlCmd.CommandText = "insert into Designation values('" + txtDesigName.Text + "','" + txtDesigDescr.Text + "')";
                        sqlCmd.Connection = sqlCon;
                        sqlCon.Open();
                        sqlCmd.ExecuteNonQuery();
                        designationTableAdapter1.Fill(designationDataSet.Designation);

                        MessageBox.Show("Successfuly Inserted");
                    }
                    else
                    {
                        //update the designation details
                        while (designationview.Rows.Count > 1)
                            designationview.Rows.RemoveAt(0);

                        sqlCmd.CommandText = "update  Designation set DesDesig='" + txtDesigName.Text + "',DesDesc='" + txtDesigDescr.Text + "' where DesId='" + txtDesigId.Text + "'";
                        sqlCmd.Connection = sqlCon;
                        sqlCon.Open();
                        sqlCmd.ExecuteNonQuery();

                        designationTableAdapter1.Fill(designationDataSet.Designation);

                        MessageBox.Show("Successfuly Updated");
                    }
                }
                catch (Exception e1)
                {
                }
                finally
                {
                    sqlCon.Close();
                    ClearControls();
                }
            }
        }


        // Function name   : btnCancel_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/17/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            btnEdit.Visible = true;
            ClearControls();
        }


        // Function name   : ClearControls
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/19/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        
        public void ClearControls()
        {
            txtDesigId.Text = "";
            txtDesigName.Text = "";
            txtDesigDescr.Text = "";
            groupBox1.Visible = false;
         
        }


        // Function name   : btnClose_Click
        // Description     : 
        // Return type     : void 
        // Author          : Priya Purushothaman
        // Date            : 10/19/2007
        // Modified By     : 
        // Modified On     : 
        // Purpose         : 
        // Argument        : object sender
        // Argument        : EventArgs e
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
